package com.vcredit.bird.service.emall;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.enumtype.BirdResponeEnum;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class JdProcessService extends BirdService {

	@Value("${jd.collect.url}")
	private String jdCollectUrl;

	@Value("${jd.crawl.url}")
	private String jdCrawlUrl;

	@Value("${jd.bill.isAuth}")
	private String JD_BILL_ISAUTH; // 实名认证

	@Log
	public BirdResponse jdCookieInit() throws Exception {
		String result = HttpRequest.doPost(jdCollectUrl + "/cookieinit", new String[] {});
		BirdResponse response = JacksonUtil.Json2Obj(result, new TypeReference<BirdResponse>() {
		});
		if (response.getCode().equals(BirdResponeEnum.HttpRequestSuccess.getCode())) {
			return response;
		} else {
			throw new BirdRequestException("京东初始化失败:" + response.getDesc());
		}
	}

	/**
	 * 
	 * @param token
	 *            令牌
	 * @param identityCard
	 *            身份证
	 * @param name
	 *            姓名
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @param cookies
	 *            cookies
	 * @param crawlerTypes
	 *            全部抓取
	 * @param busType
	 *            业务类别
	 * @param backUrl
	 *            回调url
	 * @return
	 * @throws Exception
	 */
	@Log
	public BirdResponse jdCookieLogin(String token, String identityCard, String name, String username, String password,
			String cookies, String busType, String backUrl) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Token", token);
		map.put("IdentityCard", identityCard);
		map.put("Name", name);
		map.put("Username", username);
		map.put("Password", password);
		map.put("Cookies", cookies);
		map.put("BusType", busType);
		map.put("BackUrl", backUrl);
		try {
			return super.postBirdData(jdCollectUrl + "/cookieCrawler", JacksonUtil.Obj2Json(map));
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("京东登陆异常:" + e.getMessage());
		}
	}

	@Log
	public BirdResponse jdCrawlStatus(String token) throws Exception {
		String param = "{\"Token\":\"" + token + "\"}";
		try {
			return super.postBirdData(jdCrawlUrl + "/crawlstatus", param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("根据token查询京东采集状态异常:" + e.getMessage());
		}
	}

	@Log
	public boolean jDIsAuth(String token, String name, String mobile, String identityCard) throws Exception {
		String param = "{\"Token\":\"" + token + "\",\"Name\":\"" + name + "\",\"Mobile\":\"" + mobile
				+ "\",\"IdentityCard\":\"" + identityCard + "\"}";
		BirdResponse response = null;
		try {
			response = super.postBirdData(JD_BILL_ISAUTH, param);
			return Boolean.valueOf(response.getResult());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("根据token查询京东实名信息异常:" + e.getMessage());
		}
	}
}
